﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DepartmentOnline.Models;

namespace DepartmentOnline
{
    public partial class AddInstructor : System.Web.UI.Page
    {
        protected Instructor newPerson;
        private string photopath;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["InAddPhoto"] != null)
                photopath = Session["InAddPhoto"].ToString();
            else photopath = @"/images/users/empty_photo.gif";
            newPerson = new Instructor()
                            {
                                Description = "asdsad",
                                Fio = FIOTB.Text,
                                Email = EmailTB.Text,
                                Photo = photopath
                            };
            lbName.Text = FIOTB.Text;
        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            // Save the uploaded file to an "Uploads" directory
            // that already exists in the file system of the 
            // currently executing ASP.NET application.  
            // Creating an "Uploads" directory isolates uploaded 
            // files in a separate directory. This helps prevent
            // users from overwriting existing application files by
            // uploading files with names like "Web.config".
            string saveDir = @"\Images\Users\";

            // Get the physical file system path for the currently
            // executing application.
            string appPath = Request.PhysicalApplicationPath;

            // Before attempting to save the file, verify
            // that the FileUpload control contains a file.
            if (PhotoUp.HasFile)
            {
                //string savePath = appPath + saveDir +
                //    Server.HtmlEncode(PhotoUp.FileName);
                string relativePath = saveDir +
                                  Server.HtmlEncode(Guid.NewGuid().ToString().Substring(0, 8) + @".jpg");
                string savePath = appPath + relativePath;

                // Call the SaveAs method to save the 
                // uploaded file to the specified path.
                // This example does not perform all
                // the necessary error checking.               
                // If a file with the same name
                // already exists in the specified path,  
                // the uploaded file overwrites it.
                PhotoUp.SaveAs(savePath);
                Session["InAddPhoto"] = relativePath;

                // Notify the user that the file was uploaded successfully.
                UploadStatusLabel.Text = "Your file was uploaded successfully.";
            }
            else
            {
                // Notify the user that a file was not uploaded.
                UploadStatusLabel.Text = "You did not specify a file to upload.";
                Session["InAddPhoto"] = "";
            }
        }

        protected void FinishButton_Click(object sender, WizardNavigationEventArgs e)
        {
            using (var context = new departmentEntities())
            {
                if (newPerson != null)
                {
                    try
                    {
                        context.Instructors.Add(newPerson);
                        context.SaveChanges();
                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                System.Diagnostics.Trace.WriteLine(validationError.PropertyName+" "+validationError.ErrorMessage);
                            }
                        }
                    }
                }
            }

        }

        protected void RedirectBack(object sender, EventArgs e)
        {
            Response.Redirect("Staff.aspx");
        }
    }
}